<!-- 新增编辑车辆 -->
<template>
  <el-dialog
    title="添加车辆"
    :visible.sync="dialogFormVisible"
  >
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="dataRules"
    >
      <el-form-item
        label="车辆类型"
        prop="truckTypeId"
        label-width="100px"
      >
        <el-select
          v-model="dataForm.truckTypeId"
          style="width: 100%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="车牌号码"
        prop="licensePlate"
        label-width="100px"
      >
        <el-input
          v-model="dataForm.licensePlate"
          placeholder="请输入车牌号码"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="GPS设备ID"
        prop="deviceGpsId"
        label-width="100px"
      >
        <el-input
          v-model="dataForm.deviceGpsId"
          placeholder="请输入GPS设备ID"
        ></el-input>
      </el-form-item>
    </el-form>
    <div
      slot="footer"
      class="dialog-footer"
      style="text-align: center;"
    >
      <el-button @click="closeDialog">取 消</el-button>
      <el-button
        type="warning"
        @click="addCar"
      >添加</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { getCarTypeList, truckTypeAdd } from '@/api/transit'
export default {
  name: 'VehicleAdd',
  data() {
    return {
      dialogFormVisible: false, // 弹框开关
      dataForm: {
        licensePlate: '', // 车牌号码
        deviceGpsId: '', // GPS设备ID
        truckTypeId: null // 车辆类型ID
      },
      dataRules: {
        licensePlate: [
          { required: true, message: '请输入车牌号码', trigger: 'blur' }
        ],
        deviceGpsId: [
          { required: true, message: '请输入GPS设备ID', trigger: 'blur' }
        ],
        truckTypeId: [
          { required: true, message: '请选择车辆类型', trigger: ['blur', 'change'] }
        ]
      },
      typeList: [], // 类型列表
      isShowNum: ''
    }
  },
  created() {
    this.getTypeList()
  },
  methods: {
    // 关闭弹框
    closeDialog() {
      // 关闭添加框
      this.dialogFormVisible = false
      // 清空并重置表单
      this.dataForm = {
        licensePlate: '', // 车牌号码
        deviceGpsId: '', // GPS设备ID
        truckTypeId: null // 车辆类型ID
      }
      this.$refs.dataForm.resetFields()
    },
    // 获取类型下拉菜单
    async getTypeList() {
      const res = await getCarTypeList()
      this.typeList = res.data
    },
    // 添加 & 编辑
    async addCar() {
      this.$refs.dataForm.validate(async valid => {
        if (valid) {
          // 校验通过后的逻辑
          const res = await truckTypeAdd(this.dataForm)
          if (res.code === 200) {
          // 弹窗提示成功
            this.$message.success('添加成功')
            // 关闭弹框
            this.closeDialog()
            // 重新拉取列表
            this.$emit('getList')
          }
        }
      })
    }
  }
}
</script>
<style scoped lang="scss">

</style>
